package com.kun.portal.webframe.controller;

import com.kun.portal.webframe.dao.*;
import com.kun.portal.webframe.po.*;
import com.kun.portal.webframe.service.AppointmentService;
import com.kun.portal.webframe.service.EquipmentService;
import com.kun.portal.webframe.service.FlightPlaneService;
import com.kun.portal.webframe.service.MaintenancePlanService;
import com.kun.portal.webframe.util.LazyIgnore;
import com.kun.portal.webframe.util.UserSessionUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by Administrator on 2018/7/6.
 */

@Controller

///api/maintenancePlan/query
@RequestMapping({"/api/maintenancePlan"})

public class MaintenancePlanControlller {
    private static final Logger LOG = LoggerFactory.getLogger(FwUserController.class);
    @Autowired
    private MaintenancePlanDao maintenancePlanDao;

    @Autowired
    private  MaintenancePlanSettingDao maintenancePlanSettingDao;
    @Autowired
    private MaintenancePlanService maintenancePlanService;
    private RepairPlaneSettingDao repairPlaneSettingDao;
    @InitBinder
    public void intDate(WebDataBinder dataBinder){
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
    @RequestMapping(
            value = {"/delete"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void delete(@RequestBody MaintenancePlanPo maintenancePlanPo, HttpSession session) {
        maintenancePlanDao.delete(maintenancePlanPo);
    }

    @RequestMapping(
            value = {"/save"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public MaintenancePlanPo save(@RequestBody MaintenancePlanPo maintenancePlanPo, HttpSession session) {
        if(maintenancePlanPo.getEquipmentPo()!=null){
            if(StringUtils.isBlank(maintenancePlanPo.getEquipmentPo().getId())){
                maintenancePlanPo.setEquipmentPo(null);
            }
        }
        if(maintenancePlanPo.getFlightInfoPo()!=null){
            if(StringUtils.isBlank(maintenancePlanPo.getFlightInfoPo().getFlightInfoId())){
                maintenancePlanPo.setFlightInfoPo(null);
            }
        }
        return maintenancePlanDao.save(maintenancePlanPo);
    }

    @RequestMapping(
            value = {"/query"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    @Transactional
    public Page<MaintenancePlanPo> query(@RequestParam (required = false) String filter,String status,Pageable pageable,Boolean isFlight,Date startDate ,Date endDate, HttpSession session) {

        return maintenancePlanService.find(filter,status,isFlight,startDate,endDate,pageable);
    }

}

